package cn.imvc.app.models;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Group;
import cn.imvc.app.repositories.GroupRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
@Service
public class GroupModelImpl implements GroupModel {

	@Autowired
	private GroupRepository groupRepository;
	public Page<Group> find(ViewRequest viewRequest) {
		return groupRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public Group get(int id) {
		return groupRepository.findOne(id);
	}

	public Group save(Group group) {
		return groupRepository.saveAndFlush(group);
	}

	public Group update(int id,Group group) {
		Group loadGroup=get(id);
		BeanUtils.copyProperties(group, loadGroup, "id");
		return groupRepository.saveAndFlush(loadGroup);
	}

	public void delete(int id) {
		groupRepository.delete(id);
		
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
		
	}

	public List<Group> find() {
		return groupRepository.findAll(new Sort(Direction.DESC,"order"));
	}

}
